//
//  MainViewController.m
//  PageViewControlerDemo
//
//  Created by lgq on 2017/2/25.
//  Copyright © 2017年 liukun. All rights reserved.
//

#import "MainViewController.h"
#import "MovieController.h"
#import "MusicController.h"
#import "BookController.h"

@interface MainViewController ()<UIPageViewControllerDataSource>
{
    UIPageViewController *_pageViewController;
    MovieController *_movieController;
    MusicController *_musicController;
    BookController *_bookController;
    UIImageView *_sliderImageView;
    
    NSArray *controllsers;
    
    int lastPage;
    int currentPage;
}
@property (weak, nonatomic) IBOutlet UIView *sliderView;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    lastPage = 0;
    currentPage = 0;
    
    _pageViewController = self.childViewControllers.firstObject;
    _movieController = [self.storyboard instantiateViewControllerWithIdentifier:@"MovieControllerID"];
    _musicController = [self.storyboard instantiateViewControllerWithIdentifier:@"MusicControllerID"];
    _bookController = [self.storyboard instantiateViewControllerWithIdentifier:@"BookControllerID"];
    
    controllsers = @[_movieController,_musicController,_bookController];
    
    _pageViewController.dataSource = self;
    [_pageViewController setViewControllers:@[_movieController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    _sliderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -1, self.view.frame.size.width/3.0, 3.0)];
    _sliderImageView.image = [UIImage imageNamed:@"slider"];
    [self.sliderView addSubview:_sliderImageView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentPageChanged:) name:@"currentPageChanged" object:nil];
}

- (void)currentPageChanged:(NSNotification*)notification
{
    [self setcurrentPage:[notification.object intValue]];
}

- (IBAction)changeCurrentPage:(UIButton *)sender {
    [self setcurrentPage:(int)sender.tag-100];
}

- (void)setcurrentPage:(int)page
{
    currentPage = page;
    float offset = self.view.frame.size.width/3.0*(float)currentPage;
    CGRect frame = _sliderImageView.frame;
    frame.origin = CGPointMake(offset, -1);
    [UIView animateWithDuration:0.3 animations:^{
        _sliderImageView.frame = frame;
    }];
    if (currentPage > lastPage) {
        [_pageViewController setViewControllers:@[[controllsers objectAtIndex:currentPage]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    }
    else{
        [_pageViewController setViewControllers:@[[controllsers objectAtIndex:currentPage]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
    }
    lastPage = currentPage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    if ([viewController isEqual:_musicController]) {
        return _movieController;
    }
    else if ([viewController isEqual:_bookController]){
        return _musicController;
    }
    return nil;
}
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    if ([viewController isEqual:_movieController]) {
        return _musicController;
    }
    else if ([viewController isEqual:_musicController]){
        return _bookController;
    }
    return nil;
}

@end
